#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>

void printset(const char *ch, sigset_t &set)
{
    std::cout << "pid: " << getpid() << ch;
    for (int i = 31; i >= 1; i--)
    {
        if (sigismember(&set, i))
        {
            std::cout << 1;
        }
        else
        {
            std::cout << 0;
        }
    }
    std::cout << std::endl;
}

void sig2(int sig)
{
    std::cout << "signal: " << sig << "  pid: " << getpid() << std::endl;
}

void test1()
{
    struct sigaction act, oldact;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;
    act.sa_handler = sig2;
    sigaction(SIGINT, &act, &oldact);
    printset("act    ", act.sa_mask);
    printset("oldact ", oldact.sa_mask);
    while (1)
        ;
}

void sig17(int sig)
{
    std::cout << "signal: " << sig << "  pid: " << getpid() << std::endl;
    while (1)
    {
        int n = waitpid(-1, 0, 0);
        std::cout << n << std::endl;
        if (n == -1)
        {
            break;
        }
    }
}

int main()
{
    // test1();

    // signal(17, sig17);

    signal(17, SIG_IGN);
    int n = 10;
    while (n--)
    {
        int pid = fork();
        if (pid == 0)
        {
            std::cout << "child pid: " << getpid() << std::endl;
            if (n > 5)
            {
                sleep(5);
            }
            else
            {
                sleep(3);
            }
            std::cout << "child pid: " << getpid() << std::endl;

            exit(1);
        }
    }
    while (1)
    {
        //    std::cout << "parent pid: " << getpid() << std::endl;
        sleep(1);
    }

    return 0;
}
